package com.gskj.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.gskj.commonutils.ResultJson;
import com.gskj.servicebase.exceptionhandler.GuLiException;
import com.gskj.vod.service.IVodService;
import com.gskj.vod.utils.ConstantVodUtil;
import com.gskj.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author :CodingFarmer_码农  Connor(康纳)_郭
 * @date : 09:52 20.5.19
 * @Company : http://www.gskj.com
 * @Version : 1.0
 */

// 视频点播 控制器
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {


    @Autowired
    private IVodService iVodService;

    /***
     *  上传视频
     * @param file
     * @return
     */
    @PostMapping("/upload/aliyun/video")
    public ResultJson uploadAliyunVideo(MultipartFile file) {
        // 返回上传视频 唯一id
        String videoId = iVodService.uploadVideoAly(file);
        return ResultJson.success().data("videoId", videoId);
    }

    /***
     *   根据 id 删除单个视频
     * @param id
     * @return
     */
    @DeleteMapping("/remove/aliyun/video/{id}")
    public ResultJson removeAliyunVideo(@PathVariable("id") String id) {
        try {
            // 初始化对象
            DefaultAcsClient defaultAcsClient = InitVodClient.initVodClient(ConstantVodUtil.ACCESS_KEY_ID, ConstantVodUtil.ACCESS_KEY_SECRET);
            // 创建 删除视频的 request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置要删除的视频 id
            request.setVideoIds(id);
            // 调用defaultAcsClient 方法
            defaultAcsClient.getAcsResponse(request);

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuLiException(20001, "删除视频失败");
        }
        return ResultJson.success();
    }

    /***
     * 根据ids 批量删除多个 视频
     * @param videoIdList
     * @return
     */
    @DeleteMapping("/remove/batch/aliyun/video/{ids}")
    public ResultJson removeBatchVideoByIds(
            @RequestParam(value = "ids") List<String> videoIdList) {

        // 通过id 批量视频
        iVodService.removeBatchVideoById(videoIdList);

        return ResultJson.success();
    }

    /***
     *  根据 视频id 获取 该视频授权码，用于播放视频
     * @param vid
     * @return
     */
    @GetMapping("/get/player/auth/{vid}")
    public ResultJson getPlayerAuthByVid(@PathVariable("vid") String vid) {

        try {
            //获取 视频点播对象
            DefaultAcsClient acsClient = InitVodClient.initVodClient(ConstantVodUtil.ACCESS_KEY_ID, ConstantVodUtil.ACCESS_KEY_SECRET);

            // 创建获取 视频凭证的 request、response对象
            GetVideoPlayAuthRequest authRequest = new GetVideoPlayAuthRequest();

            // 设置 视频id
            authRequest.setVideoId(vid);

            // 获取 视频响应对象
            GetVideoPlayAuthResponse acsResponse =
                    acsClient.getAcsResponse(authRequest);

            // 获取凭证
            String playAuth = acsResponse.getPlayAuth();
            return ResultJson.success().data("playAuth", playAuth);

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuLiException(20001, "获取视频凭证失败，vid=" + vid);
        }
    }
}
